from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.contrib.auth.models import User
from django.http import HttpResponse, Http404, HttpResponseRedirect
from google.appengine.ext import db
g_v={'LBFORUM_TITLE':'LBForum','LBFORUM_SUB_TITLE':'A Clone of FluxBB',}
def my_get_object_or_404(klass, **kwargs):
    #for key in kwargs.keys:
    key=kwargs.keys()[0]
    if key == "pk":
        obj=db.get(kwargs.get(key))
    else:
        q_klass=klass.all()
        q_klass.filter(key+' = ', kwargs.get(key))
        obj=q_klass.get()
    if obj is None:
        raise Http404(' %s with %s=%s not found!' % (klass._meta.object_name,key,str(kwargs.get(key))))
        
    return obj

def profile(request, user_id=None, template_name="account/profile.html"):
    view_user = request.user
    if user_id:
        #view_user = get_object_or_404(User, pk = user_id)
        view_user = my_get_object_or_404(User, pk = user_id)
    view_only = view_user != request.user
    ext_ctx = {'view_user':view_user, 'view_only':view_only }
    #ext_ctx.update(g_v)
    return render_to_response(template_name, ext_ctx, RequestContext(request))
